//Name: Final project
//Members: DungAnh-PhuongAnh-NhaUyen

#include <iostream>
using namespace std;
#include <string>
#include "BookBorrowing.h"
#include <time.h>


void BookBorrowingInfo::addInfo(char *username,int status,char *name,char* ISBN)
{
	strcpy(this->username,username);
	this->status = status;
	strcpy(this->BorrowingBook[0].Name,name);
	strcpy(this->BorrowingBook[0].ISBN,ISBN);
	setDeadline(this->BorrowingBook[0]);
	cout << "The deadline of this book is: ";
	showDeadline(0);
	setDayborrow(this->BorrowingBook[0]);
	setDayreturn(this->BorrowingBook[0]);
	this->BorrowingBook[0].isUsing = true;
	for (int i = 1;i<5;++i)
	{
		this->BorrowingBook[i].isUsing = false;
	}
	this->amount = 0;
}

void BookBorrowingInfo::addInfo(int i,char *name,char* ISBN)
{
	strcpy(this->BorrowingBook[i].Name,name);
	strcpy(this->BorrowingBook[i].ISBN,ISBN);
	setDeadline(this->BorrowingBook[i]);
	cout << "The deadline of this book is: ";
	showDeadline(i);
	setDayborrow(this->BorrowingBook[i]);
	setDayreturn(this->BorrowingBook[i]);
	this->BorrowingBook[i].isUsing = true;
}

//find deadline
void BookBorrowingInfo::setDeadline(Book &b)
{
	tm time;
	_getsystime(&time); // get system time

	if(time.tm_mon==1||time.tm_mon==3||time.tm_mon==5||time.tm_mon==7||time.tm_mon==8||time.tm_mon==10||time.tm_mon==12)
	{
		if(time.tm_mday<=16)
		{
			b.Deadline.day = time.tm_mday+15;
			b.Deadline.month = time.tm_mon+1;
			b.Deadline.year = time.tm_year+1900;
		}
		else
		{
			if(time.tm_mon==12)
			{
				b.Deadline.day = 15 - (31-time.tm_mday);
				b.Deadline.month = 1;
				b.Deadline.year = time.tm_year+1;
			}
			else
			{
				b.Deadline.day = 15 - (31-time.tm_mday);
				b.Deadline.month = time.tm_mon+2;
				b.Deadline.year = time.tm_year+1900;
			}
		}
	}
	else
	{
		if(time.tm_mon==2)
		{
			if((time.tm_year%4==0&&time.tm_year%100!=0)||(time.tm_year%400==0))//leap year
			{
				if(time.tm_mday<=14)
				{
					b.Deadline.day = time.tm_mday+15;
					b.Deadline.month = time.tm_mon+1;
					b.Deadline.year = time.tm_year+1900;
				}
				else
				{
					b.Deadline.day = 15 - (29-time.tm_mday);
					b.Deadline.month = time.tm_mon+2;
					b.Deadline.year = time.tm_year+1900;
				}
			}
			else
			{
				if(time.tm_mday<=13)
				{
					b.Deadline.day = time.tm_mday+15;
					b.Deadline.month = time.tm_mon+1;
					b.Deadline.year = time.tm_year+1900;
				}
				else
				{
					b.Deadline.day = 15 - (28-time.tm_mday);
					b.Deadline.month = time.tm_mon+2;
					b.Deadline.year = time.tm_year+1900;
				}
			}
		}
		else
		{
			if(time.tm_mday<=15)
			{
				b.Deadline.day = time.tm_mday+15;
				b.Deadline.month = time.tm_mon+1;
				b.Deadline.year = time.tm_year+1900;
			}
			else
			{
				b.Deadline.day = 15 - (30-time.tm_mday);
				b.Deadline.month = time.tm_mon+2;
				b.Deadline.year = time.tm_year+1900;
			}
		}
	}
}

void BookBorrowingInfo::setDayborrow(Book &b)
{
	tm time;
	_getsystime(&time); // get system time

	b.Dayborrow.day = time.tm_mday;
	b.Dayborrow.month = time.tm_mon+1;
	b.Dayborrow.year = time.tm_year+1900;
}

void BookBorrowingInfo::setDayreturn(Book &b)
{
	tm time;
	_getsystime(&time); // get system time

	b.Dayreturn.day = time.tm_mday;
	b.Dayreturn.month = time.tm_mon+1;
	b.Dayreturn.year = time.tm_year+1900;
}

void BookBorrowingInfo::copyInfo(BookBorrowingInfo b)
{
	strcpy(this->username,b.username);
	this->status = b.status;
	this->amount = b.amount;
	for (int i=0;i<5;++i)
	{
		if (b.BorrowingBook[i].isUsing == false)
		{
			this->BorrowingBook[i].isUsing = false;
		}
		else
		{
			strcpy(this->BorrowingBook[i].Name,b.BorrowingBook[i].Name);
			strcpy(this->BorrowingBook[i].ISBN,b.BorrowingBook[i].ISBN);
			this->BorrowingBook[i].isUsing = b.BorrowingBook[i].isUsing;
			this->BorrowingBook[i].Deadline.day = b.BorrowingBook[i].Deadline.day;
			this->BorrowingBook[i].Deadline.month = b.BorrowingBook[i].Deadline.month;
			this->BorrowingBook[i].Deadline.year = b.BorrowingBook[i].Deadline.year;

			this->BorrowingBook[i].Dayborrow.day = b.BorrowingBook[i].Dayborrow.day;
			this->BorrowingBook[i].Dayborrow.month = b.BorrowingBook[i].Dayborrow.month;
			this->BorrowingBook[i].Dayborrow.year = b.BorrowingBook[i].Dayborrow.year;

			this->BorrowingBook[i].Dayreturn.day = b.BorrowingBook[i].Dayreturn.day;
			this->BorrowingBook[i].Dayreturn.month = b.BorrowingBook[i].Dayreturn.month;
			this->BorrowingBook[i].Dayreturn.year = b.BorrowingBook[i].Dayreturn.year;
		}
	}
	for (int i=0;i<b.amount;++i)
	{
		strcpy(this->ReturnedBook[i].Name,b.ReturnedBook[i].Name);
		strcpy(this->ReturnedBook[i].ISBN,b.ReturnedBook[i].ISBN);
		this->ReturnedBook[i].isUsing = b.ReturnedBook[i].isUsing;
		this->ReturnedBook[i].Deadline.day = b.ReturnedBook[i].Deadline.day;
		this->ReturnedBook[i].Deadline.month = b.ReturnedBook[i].Deadline.month;
		this->ReturnedBook[i].Deadline.year = b.ReturnedBook[i].Deadline.year;

		this->ReturnedBook[i].Dayborrow.day = b.ReturnedBook[i].Dayborrow.day;
		this->ReturnedBook[i].Dayborrow.month = b.ReturnedBook[i].Dayborrow.month;
		this->ReturnedBook[i].Dayborrow.year = b.ReturnedBook[i].Dayborrow.year;

		this->ReturnedBook[i].Dayreturn.day = b.ReturnedBook[i].Dayreturn.day;
		this->ReturnedBook[i].Dayreturn.month = b.ReturnedBook[i].Dayreturn.month;
		this->ReturnedBook[i].Dayreturn.year = b.ReturnedBook[i].Dayreturn.year;
	}
}

char * BookBorrowingInfo::getUsername()
{
	return this->username;
}

int BookBorrowingInfo::getStatus()
{
	return this->status;
}

void BookBorrowingInfo::setisUsing( bool b,int i)
{
	this->BorrowingBook[i].isUsing = b;
}

int BookBorrowingInfo::isFullBB()
{
	for (int i=0;i<5;++i)
	{
		if (this->BorrowingBook[i].isUsing == false)
		{
			return i;
		}
	}
	return 5; // 5 meaning full
	
}

void BookBorrowingInfo::showDeadline(int i)
{
	cout << this->BorrowingBook[i].Deadline.day << "/" <<
		this->BorrowingBook[i].Deadline.month << "/" <<
		this->BorrowingBook[i].Deadline.year << endl;
}

void BookBorrowingInfo::showDayborrow( int i )
{
	cout << this->BorrowingBook[i].Dayborrow.day << "/" <<
		this->BorrowingBook[i].Dayborrow.month << "/" <<
		this->BorrowingBook[i].Dayborrow.year << endl;
}

void BookBorrowingInfo::showDayborrowofReturnBook(int i)
{
	cout << this->ReturnedBook[i].Dayborrow.day << "/" <<
		this->ReturnedBook[i].Dayborrow.month << "/" <<
		this->ReturnedBook[i].Dayborrow.year << endl;
}
void BookBorrowingInfo::showDayreturn(int i)
{
	cout << this->ReturnedBook[i].Dayreturn.day << "/" <<
		this->ReturnedBook[i].Dayreturn.month << "/" <<
		this->ReturnedBook[i].Dayreturn.year << endl;
}

void BookBorrowingInfo::showBorrowingBook()
{
	int j = 1;
	for (int i = 0;i<5;++i)
	{
		if(this->BorrowingBook[i].isUsing == true)
		{
			cout << j++ << "/\n";
			cout << "Book name: " << this->BorrowingBook[i].Name << endl;
			cout << "ISBN: " << this->BorrowingBook[i].ISBN << endl;
			cout << "Day borrow: ";
			showDayborrow(i);
			cout << "Deadline: ";
			showDeadline(i);
			cout << endl;
		}
	}
}

void BookBorrowingInfo::updateReturnedBook(int i)
{
	if (i != 0 && i != 1 && i != 2 && i != 3 && i != 4)
	{
		cout << "Wrong ISBN\n";
		return;
	}
	strcpy(this->ReturnedBook[amount].Name,this->BorrowingBook[i].Name);
	strcpy(this->ReturnedBook[amount].ISBN,this->BorrowingBook[i].ISBN);

	this->ReturnedBook[amount].Deadline.day = this->BorrowingBook[i].Deadline.day;
	this->ReturnedBook[amount].Deadline.month = this->BorrowingBook[i].Deadline.month;
	this->ReturnedBook[amount].Deadline.year = this->BorrowingBook[i].Deadline.year;

	this->ReturnedBook[amount].Dayborrow.day = this->BorrowingBook[i].Dayborrow.day;
	this->ReturnedBook[amount].Dayborrow.month = this->BorrowingBook[i].Dayborrow.month;
	this->ReturnedBook[amount].Dayborrow.year = this->BorrowingBook[i].Dayborrow.year;

	setDayreturn(this->ReturnedBook[amount]);
	++amount;
	return;
}

int BookBorrowingInfo::returnBook(char *ISBN)
{
	for (int i=0;i<5;++i)
	{
		cout << this->BorrowingBook[i].ISBN <<endl;
		if (strcmp(this->BorrowingBook[i].ISBN,ISBN) == 0)
		{
			this->BorrowingBook[i].isUsing = false;
			return i;
		}
	}
	cout << "Wrong ISBN\n";
	return 5;
}

//Get amount
int BookBorrowingInfo::getAmount()
{
	return this->amount;
}

//Show returned book of user
void BookBorrowingInfo::showReturnedBook()
{
	if(this->amount==0)
	{
		cout << "Your returned book list is empty"<<endl;
		return;
	}
	int j = 1;
	for (int i = 0;i<amount;++i)
	{
		cout << j++ << "/\n";
		cout << "Book name: " << this->ReturnedBook[i].Name << endl;
		cout << "ISBN: " << this->ReturnedBook[i].ISBN << endl;
		cout << "Day borrow: ";
		showDayborrowofReturnBook(i);
		cout << "Day return: ";
		showDayreturn(i);
		cout << endl;
	}
}